<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |4.3.2010 Erstellung | Patrik Pfaffenbauer    |
 |_____________________________________________|

******************************************************************************

P3(c) */
class Project extends Base
{
	public function Project()
	{	
		$this->title = $GLOBALS['lang']->GetLanguage("Project", "title");
		$this->toolbar = true;
		$this->moduleID = 300;
		$this->permission = GetPermission($this->moduleID);
		$this->toolbarClass = "icon-48-project";
		
		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
	}
	
	private function CheckInput()
	{
		include("./_tables.php");
		
		if($_POST['name'] == "")
		{
			$this->errorText = $GLOBALS['lang']->GetLanguage("Panel", "missedMandatoryField");
			return false;
		}
		
		$query = "SELECT * FROM ".$tblproject." WHERE name='".$_POST['name']."' AND customerID=".$_POST['customerID'];
		
		if($GLOBALS['db']->Query($query)->num_rows)
		{
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
				
			if($_POST['id'] != $res->id)
			{
				$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
				$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Project", "name"), $string);
				$string = str_replace("{1}", $_POST['name'], $string);
	
				$this->errorText = $string;
				return false;
			}
		}
		
		return true;
	}
	public function CheckFastEditInput()
	{
		include("./_tables.php");
		
		if($_GET['field'] == "name")
		{
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblproject." WHERE id=".$_GET['id']));
			
			$query = $GLOBALS['db']->Query("SELECT * FROM ".$tblproject." WHERE name='".$_GET['value']."' AND customerID=".$res->customerID);
			
			if($query->num_rows)
			{
				$res = Fetch($query);
					
				if($_GET['id'] != $res->id)
				{
					$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
					$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Project", "name"), $string);
					$string = str_replace("{1}", $_GET['value'], $string);
					
					echo "//<script>
					alert('".$string."');
					Redirect('index.php?s=project');
					//</script>";
					return false;
				}
			}
		}
		
		return true;
	}
	
	public function Add()
	{		
		include("./_tables.php");
		$panel = new Panel();
		
		if($this->error)
		{
			$panel->AddRow();
			$panel->AddText($this->errorText);
		}
		
		if($_POST['search'] != "")
			$customerID = $_POST['search'];
		else
			$customerID = $_POST['customerID'];
		
		$panel->AddRow();
		$panel->AddMandatoryTextBox("name", $GLOBALS['lang']->GetLanguage("Project", "name"), $_POST['name'], 45, 50);
		$panel->AddRow();
		$panel->AddTextArea("description", $GLOBALS['lang']->GetLanguage("Common", "description"), $_POST['description'], 50, 5);
		$panel->AddRow();
		$panel->AddDateTextBox("beginDate", $GLOBALS['lang']->GetLanguage("Common", "beginDate"), $_POST['beginDate'], 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("endDate", $GLOBALS['lang']->GetLanguage("Common", "endDate"), $_POST['endDate'], 45, 30);
		$panel->AddRow();
		$panel->AddComboBox("customerID", $GLOBALS['lang']->GetLanguage("Customer", "title2"), $customerID,  $tblcustomer, "id", "name");
		
		$panel->AddHidden("action", "save");
		$panel->AddHidden("option", "project");
		
		$panel->Close();
	}
	public function Edit()
	{		
		include("./_tables.php");
		
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tblproject." WHERE id=".$_POST['id'].""));
		
		echo '<table class="adminform"><tr><td width="40%" style="vertical-align:top;">';
		
		$panel = new Panel("");
		
		if(isset($_POST['name']))
		{
			if($this->error)
			{
				$panel->AddRow();
				$panel->AddText($this->errorText);
			}
			
			$res->name = $_POST['name'];
			$res->description = $_POST['description'];
			$res->beginDate = $_POST['beginDate'];
			$res->endDate = $_POST['endDate'];
			$res->customerID = $_POST['customerID'];
		}
		
		$panel->AddRow();
		$panel->AddMandatoryTextBox("name", $GLOBALS['lang']->GetLanguage("Project", "name"), $res->name, 45, 30);
		$panel->AddRow();
		$panel->AddTextArea("description", $GLOBALS['lang']->GetLanguage("Common", "description"), $res->description, 50, 5);
		$panel->AddRow();
		$panel->AddDateTextBox("beginDate", $GLOBALS['lang']->GetLanguage("Common", "beginDate"), $res->begin, 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("endDate", $GLOBALS['lang']->GetLanguage("Common", "endDate"), $res->end, 45, 30);
		$panel->AddRow();
		$panel->AddComboBox("customerID", $GLOBALS['lang']->GetLanguage("Customer", "title2"), $res->customerID,  $tblcustomer, "id", "name");
		
		$panel->AddHidden("action", "update");
		$panel->AddHidden("option", "project");
		$panel->AddHidden("id", $_POST['id']);
				
		$panel->CloseWithoutForm();
		
		
		echo '</td><td style="vertical-align:top;">
		
		<div id="leftColumn" class="leftColumn"  style="height:300px;">
			<p><b>'.$GLOBALS['lang']->GetLanguage("Permission", "notInGroup").'</b></p>
			<div id="dropContent">';
		$query = "SELECT * FROM ".$viewusernotinproject." WHERE projectid='".$res->id."'";
		$res = $GLOBALS['db']->Query($query);
		$color = true;
		
		
		if($_POST['userinproject'] != "")
		{
			$user = explode("-", $_POST['userinproject']);
		}
		else
		{
			$user = "";
		}
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			if($user != "")
			{
				for($i = 0; $i < count($user); $i++)
				{
					if($user[$i] == "")
						continue;
					if($line->userid == $user[$i])
					{
						$userAllowed = false;	
						break;
					}
					else
					{
						$userAllowed = true;
					}
				}
				
				if(!$userAllowed)
					continue;
			}
				
			if($color)
			{
				echo '<div class="dragableBox2" lang="'.$line->userid.'" id="box'.$line->userid.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
			}	
			else
			{
				echo '<div class="dragableBox" lang="'.$line->userid.'" id="box'.$line->userid.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
			}
			
			$color = !$color;
		}
				
			echo '</div>
		</div>
			<div id="rightColumn" style="height:300px;">
			<div id="dropBox" class="dropBox" style="margin-left:10px;">
				<p><b>'.$GLOBALS['lang']->GetLanguage("Permission", "inGroup").'</b></p>
				<div id="dropContent2">';
			$query2 = "SELECT * FROM ".$viewuserinproject." WHERE projectid='".$res->id."'";	
			$res = $GLOBALS['db']->Query($query2);
			$color = true;
			echo $_POST['userinproject'];
			
			while($line = $GLOBALS['db']->Fetch($res))
			{
				if($color)
				{
					echo '<div class="dragableBox2" lang="'.$line->userid.'" id="box'.$line->userid.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
				}	
				else
				{
					echo '<div class="dragableBox" lang="'.$line->userid.'" id="box'.$line->userid.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
				}
				$color = !$color;
			}
			
			if($user != "")
			{
				for($i = 0; $i < count($user); $i++)
				{
					if($user[$i] == "")
						continue;
						
					$line = Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tbluser." WHERE id=".$user[$i]));
						
					echo '<div class="dragableBox2" lang="'.$line->id.'" id="box'.$line->id.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
				}
			}
			
			echo '
		</div><input type="hidden" name="userinproject" id="userinproject" value="'.$_POST['userinproject'].'"/>';
		echo '<input type="hidden" name="userinprojectcount" id="userinprojectcount" value="'.$count.'" />';
		$panel->CloseForm();
		
		echo '</td></tr></table>';
		
		echo "
		<script type=\"text/javascript\">

			function dropItems(idOfDraggedItem,targetId,x,y)
			{
				if(targetId=='dropBox')
				{	
					var obj = document.getElementById(idOfDraggedItem);
					if(obj.parentNode.id=='dropContent2')return;		
					document.getElementById('dropContent2').appendChild(obj);
					document.getElementById('userinprojectcount').value++;
				//	document.getElementById('userinproject').value += obj.lang+'-';
				}
				if(targetId=='leftColumn')
				{
					var obj = document.getElementById(idOfDraggedItem);
					if(obj.parentNode.id=='dropContent')return;	
					document.getElementById('dropContent').appendChild(obj);
					document.getElementById('userinprojectcount').value--;
					
					
					
					
				}
				document.getElementById('userinproject').value = '';
				var div = document.getElementById('dropContent2');
				
				alert(div.childNodes.length);
				
				for(var i = 0; i < div.childNodes.length; i++)
				{
					if(div.childNodes.item(i).lang != null)
					{
						document.getElementById('userinproject').value += div.childNodes.item(i).lang+'-';
					}
				}
				alert(document.getElementById('userinproject').value);
			}
			
			function onDragFunction(cloneId,origId)
			{
				var obj = document.getElementById(cloneId);
				obj.style.border='1px solid #000000';
				obj.style.width = '244px';
				
			}
			var dragDropObj = new DHTMLgoodies_dragDrop();
			";
		
		if($_POST['userinproject'] != "")
		{
			$user = explode("-", $_POST['userinproject']);
		}
		else
		{
			$user = "";
		}
		
		if($this->permission->editPermission)
		{
			$res = $GLOBALS['db']->Query($query);
			while($line = Fetch($res))
			{
				if($user != "")
				{
					for($i = 0; $i < count($user); $i++)
					{
						if($user[$i] == "")
							continue;
						if($line->userid == $user[$i])
						{
							$userAllowed = false;	
							break;
						}
						else
						{
							$userAllowed = true;
						}
					}
				
					if(!$userAllowed)
						continue;
				}
				
				echo "dragDropObj.addSource('box".$line->userid."',true,true,true,false,'onDragFunction');";
			}
			$res = $GLOBALS['db']->Query($query2);
			while($line = $GLOBALS['db']->Fetch($res))
			{
				echo "dragDropObj.addSource('box".$line->userid."',true,true,true,false,'onDragFunction');";
			}
			
			if($user != "")
			{
				for($i = 0; $i < count($user); $i++)
				{
					if($user[$i] == "")
						continue;
							
					echo "dragDropObj.addSource('box".$user[$i]."',true,true,true,false,'onDragFunction');";
				}
			}
		}
		
		echo "dragDropObj.addTarget('dropBox','dropItems');	
			dragDropObj.addTarget('leftColumn','dropItems');
			dragDropObj.init();
			</script>";

	}
	
	public function AnalysPost()
	{
		include("./_tables.php");
		$this->filter = $_POST['search'];
					
		if($_POST['action'] == "add")
		{
			$this->Add();
		}
		else if($_POST['action'] == "edit")
		{
			$this->Edit();
		}
		else if($_POST['action'] == "search")
		{
			$this->Paint();
		}
		else if($_POST['action'] == "paint")
		{
			$this->filter = "";
			$this->Paint();
		}
		else if($_POST['action'] == "activate")
		{
			$this->Activate();
		}
		else if($_POST['action'] == "save")
		{
			$_POST['beginDate'] = ConvertDate($_POST['beginDate'], ".");
			$_POST['endDate'] = ConvertDate($_POST['endDate'], ".");
			
			if($this->CheckInput())
			{
				$GLOBALS['db']->SaveObject($tblproject);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Add();
			}
		}
		else if($_POST['action'] == "update")
		{
			$_POST['beginDate'] = ConvertDate($_POST['beginDate'], ".");
			$_POST['endDate'] = ConvertDate($_POST['endDate'], ".");
			
			if($this->CheckInput())
			{	
				$GLOBALS['db']->UpdateObject($tblproject);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Edit();
			}
		}
		else if($_POST['action'] == "delete")
		{
			$GLOBALS['db']->Query("DELETE FROM ".$tbljobs." WHERE projectID=".$_POST['id']);
			$GLOBALS['db']->Delete($tblproject);
			$this->Paint();
		}
		else if($_POST['action'] == "addJob")
		{
			$_GET['s'] = "task";
			$task = new Task();
			$task->AddToProject($_POST['id']);
		}
		else if($_POST['action'] == "showJobs")
		{
			$_GET['s'] = "task";
			$task = new Task();
			$task->ShowTasksByProject($_POST['id']);
		}
		else
		{
			$this->Paint();
		}
	}

	public function Paint()
	{
		include("./_tables.php");
		
		echo 
		'<div id="cpanel">';

		$dataSource = $GLOBALS['db']->Query("SELECT id as id, name as value FROM ".$tblcustomer." ");
		
		$dgv = new DataGridView($this);
		$dgv->AddComboBoxFilter($dataSource, $GLOBALS['lang']->GetLanguage("Project", "filter"). ": ", $this->filter, "customerID");
		
		$dgv->OpenHeader();
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Project", "name"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "description"), "25%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "beginDate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "endDate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Customer", "title2"), "17%", true);
		
		if($this->permission->editPermission)
			$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "function"), "10%", true);
		
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "function"), "10%", true);
		
		$dgv->CloseHeader();
		
		if($this->filter != "")
		{	
			$query = "SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tblproject." WHERE ".$_POST['filterID']."=".$this->filter."";
			$res = $GLOBALS['db']->Query($query);
		}
		else
		{
			$res = $GLOBALS['db']->Query("SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tblproject);
		}
		
		if($this->permission->editPermission)
		{
			$title = "fastEdit";
			$comboBoxEdit = "comboBox-edit";
		}
		else
		{
			$comboBoxEdit = "";
			$title = "";
		}
		
		$count = 0;
		
		$query = $GLOBALS['db']->Query("SELECT id,name FROM ".$tblcustomer);
		while($line = $GLOBALS['db']->FetchRow($query))
		{
			$object[] = $line;
		}
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			$dgv->AddRow($line->id);
			$dgv->AddData($line->name, $title, "", "name");
			$dgv->AddData(ShortText($line->description, 50, " ..."), "", "", "firstName");
			$dgv->AddData($line->begin, "", "", "beginDate");
			$dgv->AddData($line->end, "", "", "endDate");
			
			$line2 = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblcustomer." WHERE id='".$line->customerID."'"));
			$dgv->object = $object;
			$dgv->object_id = $line2->id;
			$dgv->AddData($line2->name, $comboBoxEdit, "", "customerID");
			
			
			if($this->permission->editPermission)
			{
				$img = '<img src="images/add.png" border="0" height="15px" width="15px" alt="addJob" 
								onmouseover="return overlib(\''.$GLOBALS['lang']->GetLanguage("Job", "addJob").'\');" onmouseout="return nd();"/>';
				$dgv->AddMisc("", 'return ListItemTask(\'cb'.$count.'\',\'addJob\');', $img);
			}
			$img = '<img src="images/search.png" border="0" height="15px" width="15px" alt="showJob" 
							onmouseover="return overlib(\''.$GLOBALS['lang']->GetLanguage("Job", "showJobs").'\');" onmouseout="return nd();"/>';
			$dgv->AddMisc("", 'return ListItemTask(\'cb'.$count.'\',\'showJobs\');', $img);
			
			$dgv->CloseRow();
			
			$count++;
		}
		
		$dgv->CloseDataGrid();
		
		echo '</div>';
	}
	public function Toolbar()
	{
		if(!$this->allowed)
			return;
			
		PaintDefaultToolbar($this);
	}
	
	public function AddCSS()
	{
			
	}
	public function AddJS()
	{
		echo '<script type="text/javascript" src="js/base/drag-drop.js"></script>';
	}
}

?>